$(function() {
    $('#loginBox a').on('click', function() {
        $('#loginBox').hide();
        $('#regBox').show();
    });
    $('#regBox a').on('click', function() {
        $('#loginBox').show();
        $('#regBox').hide();
    })
});


let form = layui.form;
form.verify({
    username: [
        /^[a-zA-z0-9]{6,10}$/,
        '用户名必须6-10为数字和字母'
    ],
    pwd: [
        /^[a-zA-z0-9]{6,12}$/,
        '密码必须6-12为数字和字母'
    ],
    //确认密码
    rePwd: function(value) {
        //判断密码的值和确认密码的值是否相同
        let pwd = $('#regBox input[name=password').val();
        if(pwd !== value) {
            return '两次密码输入不一致！'
        }
    }
});

let layer = layui.layer;
//注册
$('#formReg').on('submit', function(e) {
        e.preventDefault();
        axios({
            method: 'POST',
            url: '/api/reguser',
            data: $('#formReg').serialize(),
        }).then(res => {
            // console.log(res.data);
            if(res.data.status !== 0) {
                return layer.msg(res.data.message, {icon: 5});
            }
            layer.msg('恭喜，注册成功', {icon: 6});
            $('#regBox a').click();
            $('#formReg')[0].reset();
        });
});
//登录
$('#formLogin').on('submit',function(e) {
        e.preventDefault();
        axios({
            method: 'POST', 
            url: '/api/login',
            data: $(this).serialize()
        }).then(res => {
            // console.log(res.data);
            if(res.data.status !== 0) {
                return layer.msg(res.data.message, {icon: 5});
            }
            //成功提示
            layer.msg('恭喜，登录成功', {icon: 6});
            //页面跳转
            location.href = '/index.html';
            //保存tocken，后面用于身份认证
            localStorage.setItem('token', res.data.token);
        })
});
